﻿using God.ShoppingWebApi.Entities;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace God.ShoppingWebApi.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class ShoppingWebApiDbContext : 
        AbpDbContext<ShoppingWebApiDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */
        
        #region Entities from the modules
        
        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        
        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        public DbSet<UserLogin> User  { get; set; }
        public DbSet<CommodInfo> commodInfos { get; set; }
        public DbSet<CommodType> commodTypes { get; set; }
        public DbSet<ShoppingCare> shoppingCares { get; set; }
        public DbSet<OrderItem> orderItems { get; set; }
        public DbSet<Order>  orders{ get; set; }

        #endregion

        public ShoppingWebApiDbContext(DbContextOptions<ShoppingWebApiDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(ShoppingWebApiConsts.DbTablePrefix + "YourEntities", ShoppingWebApiConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});
            builder.Entity<UserLogin>(b =>
            {
                b.ToTable(ShoppingWebApiConsts.DbTablePrefix + "UserLogins", ShoppingWebApiConsts.DbSchema);
                // b.ConfigureByConvention(); //auto configure for the base class props
                //...
                b.Property("UserName").HasMaxLength(50).IsRequired();
                b.Property("UserPhone").HasMaxLength(80).IsRequired();
                b.Property("UserEmail").HasMaxLength(100);
                b.Property("UserInfoName").HasMaxLength(255);
                b.Property("UserPassword").HasMaxLength(255);
                b.Property("UserInfoRemarks").HasMaxLength(255);
            });
            builder.Entity<CommodInfo>(b =>
            {
                b.ToTable(ShoppingWebApiConsts.DbTablePrefix + "CommodInfos", ShoppingWebApiConsts.DbSchema);
                // b.ConfigureByConvention(); //auto configure for the base class props
                //...
                b.Property("CommodityName").HasMaxLength(50).IsRequired();
                b.Property("CommodityDetails").HasMaxLength(100);
                b.Property("CommodityImg").HasMaxLength(255);
                b.Property("CommodityBorwseNum").HasMaxLength(255);
                b.Property("CommoditySaleNum").HasMaxLength(255);
            });
            //商品分类
            builder.Entity<CommodType>(b =>
            {
                b.ToTable("CommodType");
                // b.ConfigureByConvention(); //auto configure for the base class props
                //...
                b.Property("CommodTypeName").HasMaxLength(50).IsRequired();
            });
            //购物车
            builder.Entity<ShoppingCare>(b =>
            {
                b.ToTable("ShoppingCare");
            });
            //订单
            builder.Entity<Order>(b =>
            {
                b.ToTable("Order");
                b.Property("OrderNo").HasMaxLength(50).IsRequired();
            });
            //订单明细
            builder.Entity<OrderItem>(b =>
            {
                b.ToTable("OrderItems");
            });
        }
    }
}
